﻿using System;
using System.Linq;
using Character;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x0200013D RID: 317
public class Accessories
{
	// Token: 0x06000725 RID: 1829 RVA: 0x0002A974 File Offset: 0x00028B74
	public Accessories(SEX sex, Transform charaRoot)
	{
		this.sex = sex;
		string name = (sex != SEX.FEMALE) ? Accessories.name_baseBoneRoot_M : Accessories.name_baseBoneRoot_F;
		this.baseBoneRoot = charaRoot.Find(name);
		this.wearsRoot = charaRoot.Find(Accessories.name_wears);
		for (int i = 0; i < this.showFlags.Length; i++)
		{
			this.showFlags[i] = true;
		}
	}

	// Token: 0x170000CE RID: 206
	// (get) Token: 0x06000726 RID: 1830 RVA: 0x0002AA00 File Offset: 0x00028C00
	public GameObject[] objAcs
	{
		get
		{
			return (from v in this.acceObjs
			select v.obj).ToArray<GameObject>();
		}
	}

	// Token: 0x06000727 RID: 1831 RVA: 0x0002AA30 File Offset: 0x00028C30
	private void Update()
	{
	}

	// Token: 0x06000728 RID: 1832 RVA: 0x0002AA34 File Offset: 0x00028C34
	public bool EnableColorCustom(int slot)
	{
		return this.acceObjs[slot] != null && this.acceObjs[slot].enableColorCustom;
	}

	// Token: 0x06000729 RID: 1833 RVA: 0x0002AA54 File Offset: 0x00028C54
	public bool EnableSecondColorCustom(int slot)
	{
		return this.acceObjs[slot] != null && this.acceObjs[slot].hasSecondColor;
	}

	// Token: 0x0600072A RID: 1834 RVA: 0x0002AA74 File Offset: 0x00028C74
	public bool EnableSecondSpecularCustom(int slot)
	{
		return this.acceObjs[slot] != null && this.acceObjs[slot].hasSecondSpecular;
	}

	// Token: 0x0600072B RID: 1835 RVA: 0x0002AA94 File Offset: 0x00028C94
	public void UpdateColorCustom(int slot)
	{
		if (this.acceObjs[slot] != null)
		{
			this.acceObjs[slot].UpdateColorCustom();
		}
	}

	// Token: 0x0600072C RID: 1836 RVA: 0x0002AAB0 File Offset: 0x00028CB0
	public void DeleteAccessory(int slot)
	{
		if (this.acceObjs[slot] != null)
		{
			this.acceObjs[slot].Destroy();
			this.acceObjs[slot] = null;
		}
	}

	// Token: 0x0600072D RID: 1837 RVA: 0x0002AAD8 File Offset: 0x00028CD8
	public void AccessoryInstantiate(AccessoryParameter acceParam, int slot, bool fixAttachParent, AccessoryData prevData)
	{
		this.DeleteAccessory(slot);
		if (acceParam.slot[slot] == null)
		{
			return;
		}
		AccessoryCustom accessoryCustom = acceParam.slot[slot];
		if (accessoryCustom == null || accessoryCustom.type == ACCESSORY_TYPE.NONE)
		{
			return;
		}
		AccessoryData acceData = CustomDataManager.GetAcceData(accessoryCustom.type, accessoryCustom.id);
		if (acceData == null)
		{
			return;
		}
		string assetName = (this.sex != SEX.FEMALE) ? acceData.prefab_M : acceData.prefab_F;
		GameObject gameObject = AssetBundleLoader.LoadAndInstantiate<GameObject>(acceData.assetbundleDir, acceData.assetbundleName, assetName);
		if (gameObject == null)
		{
			return;
		}
		GameObject gameObject2 = new GameObject("AcceParent");
		gameObject.transform.SetParent(gameObject2.transform, false);
		gameObject.transform.localPosition = Vector3.zero;
		gameObject.transform.localRotation = Quaternion.identity;
		gameObject.transform.localScale = Vector3.one;
		gameObject = gameObject2;
		if (!GlobalData.vr_event_item && acceData.special == ItemDataBase.SPECIAL.VR_EVENT)
		{
			GlobalData.vr_event_item = true;
		}
		this.acceObjs[slot] = new Accessories.AcceObj(acceParam, slot, gameObject, acceData);
		this.AttachAccessory(acceParam, slot, fixAttachParent, prevData);
		this.acceObjs[slot].SetShow(this.showFlags[slot]);
		Renderer[] componentsInChildren = gameObject.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer renderer in componentsInChildren)
		{
			renderer.gameObject.layer = this.baseBoneRoot.gameObject.layer;
			renderer.lightProbeUsage = LightProbeUsage.Off;
		}
		this.SetupDynamicBone(gameObject);
	}

	// Token: 0x0600072E RID: 1838 RVA: 0x0002AC64 File Offset: 0x00028E64
	private void SetupDynamicBone(GameObject obj)
	{
		DynamicBone[] componentsInChildren = obj.GetComponentsInChildren<DynamicBone>(true);
		DynamicBoneCollider[] componentsInChildren2 = this.baseBoneRoot.GetComponentsInChildren<DynamicBoneCollider>(true);
		foreach (DynamicBone dynamicBone in componentsInChildren)
		{
			dynamicBone.m_Colliders.Clear();
			foreach (DynamicBoneCollider item in componentsInChildren2)
			{
				dynamicBone.m_Colliders.Add(item);
			}
		}
	}

	// Token: 0x0600072F RID: 1839 RVA: 0x0002ACE0 File Offset: 0x00028EE0
	public AccessoryData GetAccessoryData(AccessoryParameter acceParam, int slot)
	{
		if (acceParam.slot[slot] == null)
		{
			return null;
		}
		AccessoryCustom accessoryCustom = acceParam.slot[slot];
		if (accessoryCustom == null || accessoryCustom.type == ACCESSORY_TYPE.NONE)
		{
			return null;
		}
		return CustomDataManager.GetAcceData(accessoryCustom.type, accessoryCustom.id);
	}

	// Token: 0x06000730 RID: 1840 RVA: 0x0002AD2C File Offset: 0x00028F2C
	public void DetachHeadAccessory(AccessoryParameter acceParam)
	{
		for (int i = 0; i < 10; i++)
		{
			AccessoryCustom accessoryCustom = acceParam.slot[i];
			if (accessoryCustom != null && this.acceObjs[i] != null && accessoryCustom.CheckAttachInHead())
			{
				this.acceObjs[i].obj.transform.SetParent(null);
			}
		}
	}

	// Token: 0x06000731 RID: 1841 RVA: 0x0002AD8C File Offset: 0x00028F8C
	public void AttachHeadAccessory(AccessoryParameter acceParam)
	{
		for (int i = 0; i < 10; i++)
		{
			this.AttachAccessory(acceParam, i, false, null);
		}
	}

	// Token: 0x06000732 RID: 1842 RVA: 0x0002ADB8 File Offset: 0x00028FB8
	public void AttachAccessory(AccessoryParameter acceParam, int slotNo, bool fixAttachParent, AccessoryData prevData)
	{
		AccessoryCustom accessoryCustom = acceParam.slot[slotNo];
		if (accessoryCustom != null && this.acceObjs[slotNo] != null)
		{
			GameObject obj = this.acceObjs[slotNo].obj;
			AccessoryData acceData = CustomDataManager.GetAcceData(accessoryCustom.type, accessoryCustom.id);
			if (acceData == null)
			{
				return;
			}
			if (acceData.defAttach == ACCESSORY_ATTACH.SKINNING)
			{
				accessoryCustom.nowAttach = acceData.defAttach;
				obj.transform.SetParent(this.wearsRoot);
				SkinnedMeshRenderer[] componentsInChildren = obj.GetComponentsInChildren<SkinnedMeshRenderer>(true);
				AttachBoneWeight.Attach(this.baseBoneRoot.gameObject, obj, true);
			}
			else
			{
				if (this.CheckAttachParentReset(accessoryCustom, prevData, acceData, fixAttachParent))
				{
					accessoryCustom.nowAttach = acceData.defAttach;
					accessoryCustom.ResetAttachPosition();
				}
				this.AttachParent(accessoryCustom, obj);
			}
		}
	}

	// Token: 0x06000733 RID: 1843 RVA: 0x0002AE7C File Offset: 0x0002907C
	private void AttachParent(AccessoryCustom acce, GameObject obj)
	{
		Transform transform = this.FindAccessoryParent(acce.nowAttach);
		obj.transform.SetParent(transform, false);
		obj.transform.localPosition = Vector3.zero;
		obj.transform.localRotation = Quaternion.identity;
		obj.transform.localScale = Vector3.one;
		if (transform == null)
		{
			GlobalData.Debug_LogWarning("配件的亲却没找到");
		}
		Transform transform2 = Transform_Utility.FindTransform(obj.transform, "N_move");
		if (transform2 == null)
		{
			GlobalData.Debug_LogWarning("N_moveがない");
			transform2 = obj.transform;
		}
		transform2.localPosition = acce.addPos;
		transform2.localRotation = Quaternion.Euler(acce.addRot);
		transform2.localScale = acce.addScl;
	}

	// Token: 0x06000734 RID: 1844 RVA: 0x0002AF44 File Offset: 0x00029144
	public bool CheckAttachParentReset(AccessoryCustom acce, AccessoryData prevData, AccessoryData nextData, bool checkDifferentType)
	{
		if (acce.nowAttach == ACCESSORY_ATTACH.NONE || acce.nowAttach == ACCESSORY_ATTACH.SKINNING)
		{
			return true;
		}
		if (!checkDifferentType)
		{
			return false;
		}
		ACCESSORY_ATTACHTYPE accessory_ATTACHTYPE = Accessories.AtttachToTypes[(int)nextData.defAttach];
		ACCESSORY_ATTACHTYPE accessory_ATTACHTYPE2 = Accessories.AtttachToTypes[(int)acce.nowAttach];
		return (prevData != null && prevData.defAttach != nextData.defAttach) || accessory_ATTACHTYPE != accessory_ATTACHTYPE2;
	}

	// Token: 0x06000735 RID: 1845 RVA: 0x0002AFB4 File Offset: 0x000291B4
	public void UpdatePosition(AccessoryParameter acceParam, int slotNo)
	{
		AccessoryCustom accessoryCustom = acceParam.slot[slotNo];
		if (accessoryCustom != null && this.acceObjs[slotNo] != null)
		{
			this.LimitPosition(accessoryCustom);
			Transform moveTrans = this.GetMoveTrans(acceParam, slotNo);
			if (moveTrans != null)
			{
				moveTrans.localPosition = accessoryCustom.addPos;
				moveTrans.localRotation = Quaternion.Euler(accessoryCustom.addRot);
				moveTrans.localScale = accessoryCustom.addScl;
			}
		}
	}

	// Token: 0x06000736 RID: 1846 RVA: 0x0002B024 File Offset: 0x00029224
	private void LimitPosition(AccessoryCustom acce)
	{
		acce.addPos.x = Mathf.Clamp(acce.addPos.x, -1f, 1f);
		acce.addPos.y = Mathf.Clamp(acce.addPos.y, -1f, 1f);
		acce.addPos.z = Mathf.Clamp(acce.addPos.z, -1f, 1f);
		acce.addRot.x = Mathf.DeltaAngle(0f, acce.addRot.x);
		acce.addRot.y = Mathf.DeltaAngle(0f, acce.addRot.y);
		acce.addRot.z = Mathf.DeltaAngle(0f, acce.addRot.z);
		acce.addScl.x = Mathf.Clamp(acce.addScl.x, 0.01f, 100f);
		acce.addScl.y = Mathf.Clamp(acce.addScl.y, 0.01f, 100f);
		acce.addScl.z = Mathf.Clamp(acce.addScl.z, 0.01f, 100f);
	}

	// Token: 0x06000737 RID: 1847 RVA: 0x0002B170 File Offset: 0x00029370
	public Transform GetMoveTrans(AccessoryParameter acceParam, int slotNo)
	{
		AccessoryCustom accessoryCustom = acceParam.slot[slotNo];
		if (accessoryCustom != null && this.acceObjs[slotNo] != null)
		{
			GameObject obj = this.acceObjs[slotNo].obj;
			AccessoryData acceData = CustomDataManager.GetAcceData(accessoryCustom.type, accessoryCustom.id);
			if (acceData == null)
			{
				return null;
			}
			if (acceData.defAttach != ACCESSORY_ATTACH.SKINNING)
			{
				Transform transform = Transform_Utility.FindTransform(obj.transform, "N_move");
				if (transform == null)
				{
					GlobalData.Debug_LogWarning("N_moveがない");
					transform = obj.transform;
				}
				return transform;
			}
		}
		return null;
	}

	// Token: 0x06000738 RID: 1848 RVA: 0x0002B200 File Offset: 0x00029400
	public void Release()
	{
		for (int i = 0; i < this.acceObjs.Length; i++)
		{
			if (this.acceObjs[i] != null)
			{
				this.acceObjs[i].Destroy();
			}
		}
	}

	// Token: 0x06000739 RID: 1849 RVA: 0x0002B240 File Offset: 0x00029440
	private Transform FindAccessoryParent(ACCESSORY_ATTACH attach)
	{
		string attachBoneName = AccessoryData.GetAttachBoneName(attach);
		Transform transform = Transform_Utility.FindTransform(this.baseBoneRoot, attachBoneName);
		if (transform == null)
		{
			GlobalData.Debug_LogError("没有配件的父母 : " + attachBoneName);
		}
		return transform;
	}

	// Token: 0x0600073A RID: 1850 RVA: 0x0002B280 File Offset: 0x00029480
	public static ACCESSORY_ATTACH GetReverseAttach(ACCESSORY_ATTACH attach)
	{
		if (attach != ACCESSORY_ATTACH.NONE)
		{
			return Accessories.ReverseAtttach[(int)attach];
		}
		return ACCESSORY_ATTACH.NONE;
	}

	// Token: 0x0600073B RID: 1851 RVA: 0x0002B294 File Offset: 0x00029494
	public void SetShow(int slotNo, bool show)
	{
		this.showFlags[slotNo] = show;
		if (this.acceObjs[slotNo] != null)
		{
			this.acceObjs[slotNo].SetShow(show);
		}
	}

	// Token: 0x0600073C RID: 1852 RVA: 0x0002B2BC File Offset: 0x000294BC
	public bool GetShow(int slotNo)
	{
		return this.showFlags[slotNo];
	}

	// Token: 0x0600073D RID: 1853 RVA: 0x0002B2C8 File Offset: 0x000294C8
	public void ChangeAllShow(bool show)
	{
		for (int i = 0; i < this.acceObjs.Length; i++)
		{
			if (this.acceObjs[i] != null)
			{
				this.acceObjs[i].SetShow(show);
			}
		}
	}

	// Token: 0x0600073E RID: 1854 RVA: 0x0002B30C File Offset: 0x0002950C
	public bool IsAttachedAccessory(int slot)
	{
		return this.acceObjs[slot] != null;
	}

	// Token: 0x04000732 RID: 1842
	private static readonly ACCESSORY_ATTACHTYPE[] AtttachToTypes = new ACCESSORY_ATTACHTYPE[]
	{
		ACCESSORY_ATTACHTYPE.HEAD,
		ACCESSORY_ATTACHTYPE.GLASSES,
		ACCESSORY_ATTACHTYPE.EAR,
		ACCESSORY_ATTACHTYPE.EAR,
		ACCESSORY_ATTACHTYPE.MOUTH,
		ACCESSORY_ATTACHTYPE.NOSE,
		ACCESSORY_ATTACHTYPE.NECK,
		ACCESSORY_ATTACHTYPE.CHEST,
		ACCESSORY_ATTACHTYPE.WRIST,
		ACCESSORY_ATTACHTYPE.WRIST,
		ACCESSORY_ATTACHTYPE.ARM,
		ACCESSORY_ATTACHTYPE.ARM,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.FINGER,
		ACCESSORY_ATTACHTYPE.LEG,
		ACCESSORY_ATTACHTYPE.LEG,
		ACCESSORY_ATTACHTYPE.ANKLE,
		ACCESSORY_ATTACHTYPE.ANKLE,
		ACCESSORY_ATTACHTYPE.NIP,
		ACCESSORY_ATTACHTYPE.NIP,
		ACCESSORY_ATTACHTYPE.WAIST,
		ACCESSORY_ATTACHTYPE.SHOULDER,
		ACCESSORY_ATTACHTYPE.SHOULDER,
		ACCESSORY_ATTACHTYPE.HAND,
		ACCESSORY_ATTACHTYPE.HAND,
		ACCESSORY_ATTACHTYPE.SKINNING
	};

	// Token: 0x04000733 RID: 1843
	private static readonly ACCESSORY_ATTACH[] ReverseAtttach = new ACCESSORY_ATTACH[]
	{
		ACCESSORY_ATTACH.AP_Head,
		ACCESSORY_ATTACH.AP_Megane,
		ACCESSORY_ATTACH.AP_Earring_R,
		ACCESSORY_ATTACH.AP_Earring_L,
		ACCESSORY_ATTACH.AP_Mouth,
		ACCESSORY_ATTACH.AP_Nose,
		ACCESSORY_ATTACH.AP_Neck,
		ACCESSORY_ATTACH.AP_Chest,
		ACCESSORY_ATTACH.AP_Wrist_R,
		ACCESSORY_ATTACH.AP_Wrist_L,
		ACCESSORY_ATTACH.AP_Arm_R,
		ACCESSORY_ATTACH.AP_Arm_L,
		ACCESSORY_ATTACH.AP_Index_R,
		ACCESSORY_ATTACH.AP_Index_L,
		ACCESSORY_ATTACH.AP_Middle_R,
		ACCESSORY_ATTACH.AP_Middle_L,
		ACCESSORY_ATTACH.AP_Ring_R,
		ACCESSORY_ATTACH.AP_Ring_L,
		ACCESSORY_ATTACH.AP_Leg_R,
		ACCESSORY_ATTACH.AP_Leg_L,
		ACCESSORY_ATTACH.AP_Ankle_R,
		ACCESSORY_ATTACH.AP_Ankle_L,
		ACCESSORY_ATTACH.AP_Tikubi_R,
		ACCESSORY_ATTACH.AP_Tikubi_L,
		ACCESSORY_ATTACH.AP_Waist,
		ACCESSORY_ATTACH.AP_Shoulder_R,
		ACCESSORY_ATTACH.AP_Shoulder_L,
		ACCESSORY_ATTACH.AP_Hand_R,
		ACCESSORY_ATTACH.AP_Hand_L,
		ACCESSORY_ATTACH.SKINNING
	};

	// Token: 0x04000734 RID: 1844
	private SEX sex;

	// Token: 0x04000735 RID: 1845
	private Transform baseBoneRoot;

	// Token: 0x04000736 RID: 1846
	private Transform wearsRoot;

	// Token: 0x04000737 RID: 1847
	private Accessories.AcceObj[] acceObjs = new Accessories.AcceObj[10];

	// Token: 0x04000738 RID: 1848
	private bool[] showFlags = new bool[10];

	// Token: 0x04000739 RID: 1849
	private static readonly string name_baseBoneRoot_F = "p_cf_anim/cf_J_Root";

	// Token: 0x0400073A RID: 1850
	private static readonly string name_baseBoneRoot_M = "p_cm_anim/cm_J_Root";

	// Token: 0x0400073B RID: 1851
	private static readonly string name_wears = "Wears";

	// Token: 0x0200013E RID: 318
	private class AcceObj
	{
		// Token: 0x06000741 RID: 1857 RVA: 0x0002B380 File Offset: 0x00029580
		public AcceObj(AccessoryParameter acceParam, int slot, GameObject obj, AccessoryData data)
		{
			this.obj = obj;
			this.acceParam = acceParam;
			this.slot = slot;
			this.SetupMaterials(data);
		}

		// Token: 0x06000742 RID: 1858 RVA: 0x0002B3A8 File Offset: 0x000295A8
		private void SetupMaterials(AccessoryData data)
		{
			this.materialCustom = this.obj.GetComponentInChildren<MaterialCustoms>();
			this.enableColorCustom = (this.materialCustom != null);
			this.hasSecondColor = false;
			this.hasSecondSpecular = false;
			if (this.enableColorCustom)
			{
				for (int i = 0; i < this.materialCustom.datas.Length; i++)
				{
					if (this.materialCustom.datas[i].param.name.IndexOf("Sub") >= 0)
					{
						this.hasSecondColor = true;
					}
					if (this.materialCustom.datas[i].param.name.IndexOf("SubMetallic") >= 0)
					{
						this.hasSecondColor = true;
						this.hasSecondSpecular = true;
						break;
					}
				}
			}
			this.UpdateColorCustom();
		}

		// Token: 0x06000743 RID: 1859 RVA: 0x0002B480 File Offset: 0x00029680
		public void UpdateColorCustom()
		{
			if (!this.enableColorCustom)
			{
				return;
			}
			if (this.acceParam.slot[this.slot].color == null)
			{
				this.acceParam.slot[this.slot].color = new ColorParameter_PBR2(this.materialCustom);
			}
			else
			{
				this.acceParam.slot[this.slot].color.SetMaterialCustoms(this.materialCustom);
			}
		}

		// Token: 0x06000744 RID: 1860 RVA: 0x0002B500 File Offset: 0x00029700
		public void Destroy()
		{
			UnityEngine.Object.Destroy(this.obj);
		}

		// Token: 0x06000745 RID: 1861 RVA: 0x0002B510 File Offset: 0x00029710
		public void SetShow(bool flag)
		{
			this.obj.SetActive(flag);
		}

		// Token: 0x0400073D RID: 1853
		public GameObject obj;

		// Token: 0x0400073E RID: 1854
		public MaterialCustoms materialCustom;

		// Token: 0x0400073F RID: 1855
		public bool enableColorCustom;

		// Token: 0x04000740 RID: 1856
		public bool hasSecondColor;

		// Token: 0x04000741 RID: 1857
		public bool hasSecondSpecular;

		// Token: 0x04000742 RID: 1858
		private AccessoryParameter acceParam;

		// Token: 0x04000743 RID: 1859
		private int slot;
	}
}
